package com.guo.hui.dao.redis;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;

public class RedisCacheUtil {

	private static RedisTemplate<String, Object> redisTemplate;

	public static Object get(String key) {
		Object value = redisTemplate.opsForValue().get(key);
		return value;
	}

	public static Collection<String> keys(String pattern) {
		return redisTemplate.keys(pattern);
	}

	public static void delete(String key) {
		redisTemplate.delete(key);
	}

	public static void delete(Collection<String> key) {
		redisTemplate.delete(key);
	}

	/* ----------- string --------- */

	public static void set(String key, Object obj) {
		if (obj == null) {
			return;
		}
		redisTemplate.opsForValue().set(key, obj);

	}

	public static void setTime(String key, Object obj, Integer timeout) {
		if (obj == null) {
			return;
		}
		if (timeout != null) {
			// TimeUnit:主要用于通知基于时间的方法如何解释给定的计时参数。
			redisTemplate.opsForValue().set(key, obj, timeout, TimeUnit.SECONDS);
		} else {
			redisTemplate.opsForValue().set(key, obj);
		}
	}

	public RedisTemplate<String, Object> getRedisTemplate() {
		return redisTemplate;
	}

	public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
		RedisCacheUtil.redisTemplate = redisTemplate;
	}

}
